package com.example.zhu95.mytimber.adapters;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.ArtistBean;
import com.example.zhu95.mytimber.listener.ArtistClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhu95
 */
public class ArtistLinearAdapter extends RecyclerView.Adapter<ArtistLinearAdapter.ViewHolder> implements SectionIndexer {
    private List<ArtistBean> artists;
    private ArtistClickListener artistClickListener;

    public void setArtists(List<ArtistBean> artists) {
        if (artists != this.artists) {
            this.artists.clear();
            this.artists.addAll(artists);
        }
    }

    public ArtistLinearAdapter(List<ArtistBean> artists, ArtistClickListener artistClickListener) {
        this.artists = artists;
        this.artistClickListener = artistClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_item_plain, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final ArtistBean artist = artists.get(position);
        holder.plainItemTv1.setText(artist.getArtist());
        holder.plainItemTv2.setText(artist.getNumOfAlbum() + " 张专辑 | " + artist.getNumOfSong() + " 首歌曲");
        // defect: 2017/12/10 艺术家的图片貌似是没有的，暂时都使用默认值
        String albumArt = artist.getArtist();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(holder.itemView.getContext()).load(albumArt).error(R.drawable.ic_empty_music2).into(holder.plainItemImg);
        } else {
            Glide.with(holder.itemView.getContext()).load(R.drawable.ic_empty_music2).centerCrop().into(holder.plainItemImg);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                artistClickListener.clickArtist(artist.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return artists.size();
    }

    @Override
    public Object[] getSections() {
        return artists.toArray();
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return sectionIndex;
    }

    @Override
    public int getSectionForPosition(int position) {
        if (position >= artists.size()) {
            position = artists.size() - 1;
        }
        return position;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.plain_item_img)
        ImageView plainItemImg;
        @BindView(R.id.plain_item_tv1)
        TextView plainItemTv1;
        @BindView(R.id.plain_item_tv2)
        TextView plainItemTv2;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
